package com.example.campuslink.commen.adpter

import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.core.CoreAdapter
import com.example.campuslink.commen.core.CoreViewHolder
import com.example.campuslink.commen.db.invitation.InvitationTable
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.databinding.AdapterShopInvitationBinding
import com.example.campuslink.viewModel.ShopViewModel

class ShopInvitationAdapter(vm: ShopViewModel) :
    CoreAdapter<AdapterShopInvitationBinding, ShopViewModel, InvitationTable>(
        BR.data,
        BR.viewModel,
        vm
    ) {
    override fun getItemLayoutId(viewType: Int): Int {
        return R.layout.adapter_shop_invitation
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val images = mListData[position].invitationImage
        if (images.isNotEmpty()) {
            Glide.with(holder.itemView.context).load(images[0])
                .error(R.drawable.ic_empty)
                .into(holder.binding.root.findViewById<ImageView>(R.id.iv_contentImg))
        } else {
            // 设置默认图片或隐藏 ImageView
            holder.binding.root.findViewById<ImageView>(R.id.iv_contentImg)
                .setImageResource(R.drawable.ic_empty)
        }
        holder.binding.root.findViewById<TextView>(R.id.tv_title).text =
            mListData[position].invitationTitle
        holder.binding.root.findViewById<TextView>(R.id.tv_userName).text =
            mListData[position].userNumber
        holder.binding.root.findViewById<TextView>(R.id.tv_have_love_count).text =
            mListData[position].collectionThisInvitationNumber.size.toString()
        if (mListData[position].userInfoTable != null) {
            Glide.with(holder.itemView.context).load(mListData[position].userInfoTable?.userAvatar)
                .error(R.drawable.ic_empty)
                .apply(RequestOptions.circleCropTransform())
                .into(holder.binding.root.findViewById<ImageView>(R.id.iv_userImg))
        }
        if (mListData[position].collectionThisInvitationNumber.contains(
                Sp.getString(
                    holder.itemView.context,
                    SpKey.USER_NUMBER.key,
                    ""
                )
            )
        ) {
            Glide.with(holder.itemView.context).load(R.drawable.ic_collected)
                .error(R.drawable.ic_empty)
                .into(holder.binding.root.findViewById<ImageView>(R.id.iv_love))
        } else {
            Glide.with(holder.itemView.context).load(R.drawable.ic_collect)
                .error(R.drawable.ic_empty)
                .into(holder.binding.root.findViewById<ImageView>(R.id.iv_love))
        }
    }
}